import hashlib
import random
import re

ALL_CHARS = '0123456789abcdefghijlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
USERNAME_PATTERN = re.compile(r'\w{6,20}')


def check_username(username):
    matcher = USERNAME_PATTERN.fullmatch(username)
    return matcher is not None


def check_password(password):
    return len(password) >= 8


def to_md5_hex(content):
    """生成MD5摘要"""
    return hashlib.md5(content.encode()).hexdigest()


def random_text(length=4):
    """生成指定长度的随机字符"""
    return ''.join(random.choices(ALL_CHARS, k=length))
